using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using nDeveloper.BO.Info;
using nDeveloper.BO.Base.Domain;
using WeifenLuo.WinFormsUI;
using WeifenLuo.WinFormsUI.Docking;

namespace nDeveloper.UI.Windows.IDE
{
    public partial class frmDefaultData : DockContent
    {
        private DataSet dsDefaultData = new DataSet();

        public frmDefaultData()
        {
            InitializeComponent();
        }

        private void frmDefaultData_Load(object sender, EventArgs e)
        {
            CarregarEntidades();
            PreencheGrid();
        }

        private void CarregarEntidades()
        {
            SystemDataDictionaryEntity se = new SystemDataDictionaryEntity();
            ArrayList are = se.GetEntityList();
            are.Sort(new SystemDataDictionaryInfoEntityComparer());

            this.cmbEntidade.DisplayMember = "Name";
            this.cmbEntidade.ValueMember = "Name";
            this.cmbEntidade.DataSource = are;
        }
        
        private void PreencheGrid()
        {
            SystemDataDictionaryDefaultValue def = new SystemDataDictionaryDefaultValue();
            dsDefaultData = def.GetListColumnsAndValues(cmbEntidade.SelectedValue.ToString());

            grdItens.DataSource = dsDefaultData.Tables[0];
            grdItens.AutoGenerateColumns = true;
        }

        private void btnNovo_Click(object sender, EventArgs e) {
            SystemDataDictionaryEntityColumns columns = new SystemDataDictionaryEntityColumns();
            
            frmAddDefaultData frm = new frmAddDefaultData();
            frm.dsDadosPadroes = ((DataTable)grdItens.DataSource).DataSet;
            frm.SelectedIndex = -1;
            frm.ListaCampos = columns.GetEntityColumnsList(cmbEntidade.SelectedValue.ToString());
            frm.EntityName = cmbEntidade.SelectedValue.ToString();

            if (frm.ShowDialog() == DialogResult.OK) {
                PreencheGrid();
            }
        }

        private void grdItens_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) {
            /*DataSet ds = new DataSet();
            ds.Tables.Add((DataTable)grdItens.DataSource);

            SystemDataDictionaryDefaultValue def = new SystemDataDictionaryDefaultValue();
            def.Save(ds, cmbEntidade.SelectedValue.ToString());*/
        }

        private void grdItens_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
            SystemDataDictionaryDefaultValue def = new SystemDataDictionaryDefaultValue();
            def.Save(((DataTable)grdItens.DataSource).DataSet, cmbEntidade.SelectedValue.ToString());
        }

        private void btnAtualizar_Click(object sender, EventArgs e) {
            PreencheGrid();
        }

        private void cmbEntidade_SelectedIndexChanged(object sender, EventArgs e) {
            PreencheGrid();
        }

        private void btnAlterar_Click(object sender, EventArgs e) {

            SystemDataDictionaryEntityColumns columns = new SystemDataDictionaryEntityColumns();
            
            frmAddDefaultData frm = new frmAddDefaultData();
            frm.dsDadosPadroes = ((DataTable)grdItens.DataSource).DataSet;
            frm.SelectedIndex = grdItens.SelectedRows[0].Index;
            frm.ListaCampos = columns.GetEntityColumnsList(cmbEntidade.SelectedValue.ToString());
            frm.EntityName = cmbEntidade.SelectedValue.ToString();

            if (frm.ShowDialog() == DialogResult.OK) {
                PreencheGrid();
            }            
        }
    }
}